package ks.packs.anotherDay;

import java.util.List;

import net.daum.mobilead.MobileAdView;

import com.admob.android.ads.AdView;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

public class SearchView extends Activity implements OnClickListener {

	private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
	PackageManager pm;

	EditText editTextSearchName;
	ImageView btnVoiceSearch;
	ImageView btnSearch;

	String sortOfDictionary;
	
	AdView adViewAdMob;
	MobileAdView adViewAdam;
	MultipleAd mAd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);

		Intent LastIntent = getIntent();
		sortOfDictionary = LastIntent
				.getStringExtra(getString(R.string.SORT_OF_DICTIONARY));

		if (HasVoiceRecognition()) {

			btnVoiceSearch = (ImageView) findViewById(R.id.btnVoiceSearch);
			btnVoiceSearch.setOnClickListener(this);

		} else {

			ksLog.d("no voice recognition");

		}

		btnSearch = (ImageView) findViewById(R.id.btnSearch);
		btnSearch.setOnClickListener(this);
		
		String countrySO = setCountryCode();

		adViewAdam = (MobileAdView) findViewById(R.id.adviewAdam);
		adViewAdMob = (AdView) findViewById(R.id.adviewAdmob);

		mAd = new MultipleAd(countrySO, adViewAdMob, adViewAdam);

		if (mAd.isKorea()) {

			mAd.initAdam(false, "308Z0CT12dd46458a1");
			mAd.displayAdam();

		} else {

			mAd.initAdMob(false);
			mAd.displayAdmob();

		}

	}
	
	private String setCountryCode() {
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

		return tm.getSimCountryIso();
	}

	public void onClick(View v) {

		if (v == btnVoiceSearch) {

			startVoiceRecognitionActivity();

		} else if (v == btnSearch) {

			editTextSearchName = (EditText) findViewById(R.id.editTextSearchName);

			Intent goSearch = new Intent(getApplicationContext(), tabView.class);

			goSearch.putExtra(getString(R.string.SORT_OF_DICTIONARY),
					sortOfDictionary);
			goSearch.putExtra("query", editTextSearchName.getText().toString());

			startActivity(goSearch);

		}
	}

	private Boolean HasVoiceRecognition() {

		pm = getPackageManager();

		List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
				RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);

		if (activities.size() != 0) {

			return true;

		} else {

			return false;

		}
	}

	private void startVoiceRecognitionActivity() {

		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

		if (sortOfDictionary.compareTo(getString(R.string.KOREAN_TO_ENG)) == 0
				|| sortOfDictionary
						.compareTo(getString(R.string.KOREAN_TO_ENG)) == 0) {

			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko");
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "한 단어를 말씀해 주세요.");

		} else {

			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
					"Please speak an English Word.");
		}

		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

		startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
				&& resultCode == RESULT_OK) {

			Intent intent = new Intent(getApplicationContext(),
					ResultView.class);
			intent.putStringArrayListExtra("data", data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS));
			intent.putExtra(getString(R.string.SORT_OF_DICTIONARY),
					sortOfDictionary);

			startActivity(intent);

		}

		super.onActivityResult(requestCode, resultCode, data);

	}
	
	protected void onResume() {

		super.onResume();

		if (mAd.isKorea()) {

			mAd.displayAdam();

		} else {

			mAd.displayAdmob();
		}

	}
}
